package com.glory.cqaospda.view

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.KeyEvent
import android.view.View
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.glory.cqaospda.R
import com.glory.cqaospda.adapter.ProcessCheckAdapter
import com.glory.cqaospda.base.Constant
import com.glory.cqaospda.base.MyApp
import com.glory.cqaospda.bean.ProcessCheckBean
import com.glory.cqaospda.viewmodel.InspectDetailsModel
import com.glory.mvvmnet.base.BaseActivity
import com.google.gson.JsonObject
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ActivityManager
import kotlinx.android.synthetic.main.activity_inspectdetails.*
import kotlinx.android.synthetic.main.activity_title.*

class InspectDetailsActivity : BaseActivity<InspectDetailsModel>() {

    var processCheckAdapter: ProcessCheckAdapter? = null
    var inspectType = ""
    var lotID = ""
    var handler: Handler = object : Handler() {
        @SuppressLint("HandlerLeak")
        override fun handleMessage(msg: Message) {
            when (msg.what) {
                0x10 -> {
                    var s: String = msg.obj as String
                    val intent = Intent()
                    intent.putExtra("type", "Error")
                    intent.putExtra("error", s)
                    val clazz = Class.forName("com.glory.cqaospda.view.SkipDialogActivity")
                    intent.setClass(this@InspectDetailsActivity, clazz)
                    startActivity(intent)
                }
            }
            super.handleMessage(msg)
        }
    }

    override fun initData() {

        viewModel.defUI.callObserve.observe(this@InspectDetailsActivity) {
            if (!Constant.okgoError) {
                when (it.type) {
                    1 -> {
                        val processCheckBean = it.item as ProcessCheckBean
                        processCheckAdapter!!.setList(processCheckBean.mItemList)
                    }
                    2 -> {
//                    viewModel.defUI.toastEvent.value = ToastMessageBean("接单成功",true)
                        val map = JsonObject()
                        map.addProperty("LotNo", lotID)
                        map.addProperty("CheckType", inspectType)
                        viewModel.defaultProcessCheck(map)
                    }
                    3 -> {
                        processCheckAdapter!!.data.clear()
                    }
                }
            } else {
                Constant.okgoError = false
            }
        }

    }

    override fun initView(savedInstanceState: Bundle?) {
        inspectType = intent.getStringExtra("inspectType").toString()
        lotID = intent.getStringExtra("lotID").toString()

        tvUserName.text = Constant.UserName
        tvBaseSubmit.visibility = View.GONE
        tvTitle.text = inspectType

        FreeApi_ActivityManager.getActivityManager().addActivity(this)

        initRecyclerView(rvProcessCheck, this)
        processCheckAdapter = ProcessCheckAdapter(null, inspectType != "buyoff")
        rvProcessCheck.adapter = processCheckAdapter

        processCheckAdapter!!.setOnItemClickListener { itemList, position ->
            when (itemList.mState) {
                "0" -> {
//                    if(Constant.UserName != itemList.mUserName){
//                        viewModel.defUI.toastEvent.value = ToastMessageBean("当前操作不允许！",false)
//                        return@setOnItemClickListener
//                    }
                    val map = JsonObject()
                    map.addProperty("UserName", Constant.UserName)
                    map.addProperty("EquipmentName", "")
                    map.addProperty("LotNo", itemList.mLotNo)
                    map.addProperty("CheckType", inspectType)
                    map.addProperty("MaintenanceItem", "")
                    map.addProperty("OrderID", itemList.mOrderID)
                    map.addProperty("CheckResult", "")
                    map.addProperty("ReasonCode", "")
                    map.addProperty("State", "0")
                    map.addProperty("Comment", "")
                    viewModel.defaultCheckOrder(map)
                }
                "1" -> {
                    intent.putExtra("itemList", itemList)
                    intent.putExtra("inspectType", inspectType)
                    intent.setClass(this, InspectDetailsItemActivity::class.java)
                    startActivityForResult(intent, 1)
                }

            }
        }

        val map = JsonObject()
        map.addProperty("LotNo", lotID)
        map.addProperty("CheckType", inspectType)
        viewModel.defaultProcessCheck(map)

        val instance = MyApp.instance
        instance!!.handler = handler
    }

    override fun layoutId(): Int {
        return R.layout.activity_inspectdetails
    }

    protected fun initRecyclerView(recyclerView: RecyclerView, context: Context?) {
        recyclerView.layoutManager = LinearLayoutManager(context)
        recyclerView.itemAnimator = DefaultItemAnimator()
        recyclerView.addItemDecoration(DividerItemDecoration(context, 1))
    }

    @SuppressLint("SetTextI18n")
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == 1) {
            when (resultCode) {
                1 -> {
                    val map = JsonObject()
                    map.addProperty("LotNo", lotID)
                    map.addProperty("CheckType", inspectType)
                    processCheckAdapter!!.setList(null)
                    viewModel.defaultProcessCheck(map)
                }
                108 -> {
                    finish()
                }
            }
        }
    }

    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            intent.putExtra("type", "KeyEvent")
            intent.setClass(this, SkipDialogActivity::class.java)
            startActivityForResult(intent, 1)
            return false
        }
        return super.onKeyUp(keyCode, event)
    }
}